<?php

namespace app\index\controller;

use think\Controller;

class Demo extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $weixin = new Weixin();

        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);

        $this->assign('jsconfig', $js_config);
    }

    public function index()
    {
        $weixin = new Weixin();

        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);

        return $this->fetch();
    }

    public function img()
    {
        if(!cmf_is_wechat()){
            $this->error('需要微信中打开');
        }

        $weixin = new Weixin();

        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);

        return $this->fetch();

    }

    /**
     * 验证身份证接口demo
     */
    public function chaeckIdCard()
    {
        $id_card = "412701199002283070";

        echo check_id_card($id_card);

    }

    /**
     * 微信接口--获取地址位置
     */
    public function location()
    {
        if(!cmf_is_wechat()){
            $this->error('需要微信中打开');
        }


        return $this->fetch();
    }


    /**
     * 测试模板
     */
    public function testWxTemplete()
    {
        $weixin = new Weixin();

        $openid         = 'oTJius6kQ2hFOF66Dw0l5Xg-ICUM';
        $templete_id    = 'BWrP-c1uXNvLvO-YNDtbVGVd0iiP7O3LibkzXkHirTI';
        $url            = 'http://www.baidu.com';

        $data_arr['first']          = ['value'=>'你的宝贝已经完成付款','color'=>'#2ee4d1'];
        $data_arr['goods']          = ['value'=>'张弓十年','color'=>'#2ee4d1'];
        $data_arr['price']          = ['value'=>'124.00 元','color'=>'#2ee4d1'];
        $data_arr['num']            = ['value'=>'5 件','color'=>'#2ee4d1'];
        $data_arr['spec']           = ['value'=>'1*6','color'=>'#2ee4d1'];

        $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr);
        if($re['errcode'] != 0){
            $this->error("错误代码".$re['errcode']);
        }
    }


    /**
     * 乘客发布
     */
    public function tripAdd()
    {
        $weixin = new Weixin();

        $openid         = 'oTJius6kQ2hFOF66Dw0l5Xg-ICUM';
        $templete_id    = 'Jxz9iOGhPUiGxX_S-4qzW19aeKY3qDOXhUCMOsARLcU';
        $url            = 'http://www.baidu.com';

        $data_arr['user_name']          = ['value'=>'亲爱的李先生，您的行程已发布，系统正在努力为你寻找顺路车主，请耐心等待~','color'=>'#ff8a5a'];
        $data_arr['begin_place']        = ['value'=>'周口市人民商场','color'=>'#1491cb'];
        $data_arr['end_place']          = ['value'=>'周口市周口师范学院','color'=>'#1491cb'];
        $data_arr['depart_time']        = ['value'=>'2018-05-12 18:30:55','color'=>'#1491cb'];
        $data_arr['number']             = ['value'=>'4','color'=>'#1491cb'];
        $data_arr['content']            = ['value'=>'有大件行李,需走高速，高速费由我承担','color'=>'#1491cb'];

        $re = $weixin->sentMsg($openid , $templete_id , $url ,$data_arr);
        if($re['errcode'] != 0){
            $this->error("错误代码".$re['errcode']);
        }
    }


    /**
     * 设置自定义菜单
     * URL: index/demo/setWxMenu
     */
    public function setWxMenu()
    {
        $weixin = new Weixin();

        $access_token = $weixin->getAccessToken();

        $jsonmenu = '
        {
            "button":[
                {
                    "type":"view",
                    "name":"自愿顺风车",
                    "url":"http://didi.aliycl.com"
                },
                {
                    "type":"view",
                    "name":"爱心送考",
                    "url":"http://didi.aliycl.com/index/love.activity"
                }
            ]
        }';

        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;

        $result = $weixin->https_request($url,$jsonmenu);

        print_r($result);



    }


    /**
     * 长连接转短链接
     */
    public function longToShortUrl()
    {
        $weixin = new Weixin();

        $access_token = $weixin->getAccessToken();

        $data = json_encode(array("action"=>"long2short","long_url"=>"http://didi.aliycl.com/index/index/center?id=2&seat_id=3&shop_id=4"));

        $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=".$access_token;

        $result = $weixin->https_request($url,$data);

        print_r($result);

    }


    /**
     * 客服消息
     */
    public function kefuMsg()
    {
        $weixin = new Weixin();

        $access_token = $weixin->getAccessToken();

        $data = '
            {
                "touser":"ohY1hwKPWfkOkD-_EFn5IFYyQsHk",
                "msgtype":"text",
                "text":
                {
                    "content":"Hello World"
                }
            }
        
        ';

        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;

        $result = $weixin->https_request($url,$data);

        print_r($result);
    }





}
